const fs = require('fs');
const crypto = require('crypto');

window.globalConfig = {
  fileInfo: {},
  secret: "",
  algorithm: 'aes-256-cbc'
}
utools.onPluginEnter(({ code, type, payload, optional }) => {
  globalConfig.fileInfo = payload;
});
window.encryptData = function (origin, target) {
  const key = crypto.scryptSync(globalConfig.secret, '', 32);
  const cipher = crypto.createCipheriv(globalConfig.algorithm, key, Buffer.alloc(16, 0));
  const input = fs.createReadStream(origin);
  const output = fs.createWriteStream(target);
  input.pipe(cipher).pipe(output);
}

window.decryptData = function (origin, target) {
  const key = crypto.scryptSync(globalConfig.secret, '', 32);
  const decipher = crypto.createDecipheriv(globalConfig.algorithm, key, Buffer.alloc(16, 0));
  const input = fs.createReadStream(origin);
  const output = fs.createWriteStream(target);
  input.pipe(decipher).pipe(output);
}